﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Business;

namespace KhoBanHang
{
    public partial class PhieuGiaoHang : Form
    {
        
        Business.PhieuGiaoHang phieuGiaoHang;
        

       
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();

        }
        

        
        public PhieuGiaoHang()
        {
            InitializeComponent();
        }

        public void LayTheoHDB(int maHDB) 
        {
            phieuGiaoHang = Business.PhieuGiaoHang.TaoPhieuGiaoHang(maHDB);
        }

        public void LayTheoPGH(int maPGH) 

        {
            phieuGiaoHang = Business.PhieuGiaoHang.LayPhieuGiaoHang(maPGH);
        }

        private void PhieuGiaoHang_Load(object sender, EventArgs e)
        {
            LoadThongTinPhieuGiaoHang();
            dtpickerNgayLap.CustomFormat = "dd/MM/yyyy";
            if(@Session.quyen=="1"||@Session.quyen=="2")
            {
                btnLuu.Enabled = false;
            }

        }


        private void LoadThongTinPhieuGiaoHang()
        {
            if (phieuGiaoHang.MaPGH != 0)
                txtMaPhieuGH.Text = phieuGiaoHang.MaPGHHienThi;

            txtMaHD.Text = phieuGiaoHang.MaHDBHienThi;

            txtNVLapPT.Text = "NV" + @Session.maNV;
            txtKhachHang.Text = phieuGiaoHang.TenKhachHang;

            txtDiaChi.Text = phieuGiaoHang.DiaChiKhachHang;

            txtDienThoai.Text = phieuGiaoHang.DienThoai;
            txtMaSoThue.Text = phieuGiaoHang.MaSoThue;
            txtCongTienHang.Text = phieuGiaoHang.TienTruocThue.ToString();

            txtTongTien.Text = phieuGiaoHang.TienSauThue.ToString();
            txtThueSuat.Text = phieuGiaoHang.ThueSuatHienThi.ToString();
            dtpickerNgayLap.Value = phieuGiaoHang.NgayGiao;

            LoadDanhSachchiTietMatHang();
        }

        private void LoadDanhSachchiTietMatHang()
        {
            BindingSource source = new BindingSource();
            source.DataSource = phieuGiaoHang.DanhSachChiTietMatHang;

            gridViewChiTietMatHang.DataSource = source;

            gridViewChiTietMatHang.RefreshEdit();
            gridViewChiTietMatHang.Refresh();

            gridViewChiTietMatHang.Columns["MaCTHDB"].Visible = false;
            gridViewChiTietMatHang.Columns["MaHang"].Visible = false;

            gridViewChiTietMatHang.Columns["MaHDB"].Visible = false;
            gridViewChiTietMatHang.Columns["ChietKhau"].Visible = false;

            gridViewChiTietMatHang.Columns["MaCTHDBHienThi"].HeaderText = "Mã chi tiết đặt";
            gridViewChiTietMatHang.Columns["MaHangHienThi"].HeaderText = "Mã hàng";
            gridViewChiTietMatHang.Columns["TenHang"].HeaderText = "Tên hàng";
            gridViewChiTietMatHang.Columns["SoLuong"].HeaderText = "Số lượng";

            gridViewChiTietMatHang.Columns["DonGiaBan"].HeaderText = "Đơn giá bán";
            gridViewChiTietMatHang.Columns["ChietKhauHienThi"].HeaderText = "Chiết khấu (%)";

            gridViewChiTietMatHang.Columns["DonGiaBan"].DefaultCellStyle.Format = "##,###,###";
        }
        

        
        private void CapNhatThongTinChung()
        {
            phieuGiaoHang.MaNV = Int16.Parse(txtNVLapPT.Text.Substring(2));
            phieuGiaoHang.NgayGiao = dtpickerNgayLap.Value;
        }



        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (phieuGiaoHang.MaPGH == 0)
            {
                CapNhatThongTinChung();
                int maPGH = Business.PhieuGiaoHang.ThemMoi(phieuGiaoHang);

                phieuGiaoHang = Business.PhieuGiaoHang.LayPhieuGiaoHang(maPGH);

                LoadThongTinPhieuGiaoHang();

                MessageBox.Show("Thêm mới phiếu giao hàng thành công");
            }
            else
            {
                CapNhatThongTinChung();
                Business.PhieuGiaoHang.CapNhat(phieuGiaoHang);
                phieuGiaoHang = Business.PhieuGiaoHang.LayPhieuGiaoHang(phieuGiaoHang.MaPGH);

                LoadThongTinPhieuGiaoHang();
                MessageBox.Show("Cập nhật phiếu giao hàng thành công");
            }
        }
        

        private void btnLuu_Click_1(object sender, EventArgs e)
        {
            if (phieuGiaoHang.MaPGH == 0)
            {
                CapNhatThongTinChung();
                int maPGH = Business.PhieuGiaoHang.ThemMoi(phieuGiaoHang);

                phieuGiaoHang = Business.PhieuGiaoHang.LayPhieuGiaoHang(maPGH);

                LoadThongTinPhieuGiaoHang();

                MessageBox.Show("Thêm mới phiếu giao hàng thành công");
            }
            else
            {
                CapNhatThongTinChung();
                Business.PhieuGiaoHang.CapNhat(phieuGiaoHang);
                phieuGiaoHang = Business.PhieuGiaoHang.LayPhieuGiaoHang(phieuGiaoHang.MaPGH);

                LoadThongTinPhieuGiaoHang();
                MessageBox.Show("Cập nhật phiếu giao hàng thành công");
            }
        }
    }
}
